/**
 * 大棚相关 API
 */
import { get, post, put, del } from '../utils/request.js'
import { API_ENDPOINTS } from '../utils/config.js'

/**
 * 获取大棚列表
 * @param {Object} params 查询参数
 * @param {number} params.page 页码，从0开始
 * @param {number} params.size 每页大小
 * @param {string} params.sort 排序字段和方向
 * @returns {Promise} 大棚列表
 */
export const getGreenhouseList = (params = {}) => {
  const defaultParams = {
    page: 0,
    size: 10,
    sort: 'createdAt,desc'
  }
  return get(API_ENDPOINTS.GREENHOUSE.LIST, { ...defaultParams, ...params })
}

/**
 * 创建大棚
 * @param {Object} greenhouseData 大棚数据
 * @param {string} greenhouseData.name 大棚名称
 * @param {string} greenhouseData.crop 种植作物
 * @param {number} greenhouseData.area 大棚面积
 * @param {string} greenhouseData.type 大棚类型
 * @param {string} greenhouseData.remark 备注信息
 * @returns {Promise} 创建结果
 */
export const createGreenhouse = (greenhouseData) => {
  return post(API_ENDPOINTS.GREENHOUSE.CREATE, greenhouseData)
}

/**
 * 获取大棚详情
 * @param {number} greenhouseId 大棚ID
 * @returns {Promise} 大棚详情
 */
export const getGreenhouseDetail = (greenhouseId) => {
  const url = API_ENDPOINTS.GREENHOUSE.DETAIL.replace('{id}', greenhouseId)
  return get(url)
}

/**
 * 更新大棚信息
 * @param {number} greenhouseId 大棚ID
 * @param {Object} greenhouseData 大棚数据
 * @returns {Promise} 更新结果
 */
export const updateGreenhouse = (greenhouseId, greenhouseData) => {
  const url = API_ENDPOINTS.GREENHOUSE.UPDATE.replace('{id}', greenhouseId)
  return put(url, greenhouseData)
}

/**
 * 删除大棚
 * @param {number} greenhouseId 大棚ID
 * @returns {Promise} 删除结果
 */
export const deleteGreenhouse = (greenhouseId) => {
  const url = API_ENDPOINTS.GREENHOUSE.DELETE.replace('{id}', greenhouseId)
  return del(url)
}

/**
 * 获取大棚最新传感器数据
 * @param {number} greenhouseId 大棚ID
 * @returns {Promise} 传感器数据
 */
export const getSensorData = (greenhouseId) => {
  const url = API_ENDPOINTS.GREENHOUSE.SENSOR_DATA.replace('{id}', greenhouseId)
  return get(url)
}

/**
 * 获取大棚所有传感器数据
 * @param {number} greenhouseId 大棚ID
 * @param {Object} params 查询参数
 * @param {string} params.startTime 开始时间，ISO格式
 * @param {string} params.endTime 结束时间，ISO格式
 * @param {number} params.page 页码，从0开始
 * @param {number} params.size 每页大小
 * @returns {Promise} 传感器数据列表
 */
export const getAllSensorData = (greenhouseId, params = {}) => {
  const url = API_ENDPOINTS.GREENHOUSE.ALL_SENSOR_DATA.replace('{id}', greenhouseId)
  const defaultParams = {
    page: 0,
    size: 10
  }
  return get(url, { ...defaultParams, ...params })
}
